﻿using System;
using System.Windows.Forms;

namespace BrowserXP
{
    /// <summary>
    /// 
    /// </summary>
    public class ExtendedNotifyIcon : IDisposable
    {
        public NotifyIcon targetNotifyIcon;
        private System.Drawing.Point notifyIconMousePosition;
        private Timer delayMouseLeaveEventTimer;

        public delegate void MouseLeaveHandler();
        public event MouseLeaveHandler MouseLeave;

        public delegate void MouseMoveHandler();
        public event MouseMoveHandler MouseMove;

        /// <summary>
        /// </summary>
        /// <param name="millisecondsToDelayMouseLeaveEvent"></param>
        public ExtendedNotifyIcon(int millisecondsToDelayMouseLeaveEvent)
        {
            targetNotifyIcon = new NotifyIcon();
            targetNotifyIcon.Visible = true;
            targetNotifyIcon.MouseMove += new MouseEventHandler(targetNotifyIcon_MouseMove);

            delayMouseLeaveEventTimer = new Timer();
            delayMouseLeaveEventTimer.Tick += new EventHandler(delayMouseLeaveEventTimer_Tick);
            delayMouseLeaveEventTimer.Interval = 100;
        }

        /// <summary>
        /// 
        /// </summary>
        public ExtendedNotifyIcon() : this(100) { }

        /// <summary>
        /// 
        /// </summary>
        public void StartMouseLeaveTimer()
        {
            delayMouseLeaveEventTimer.Start();
        }

        /// <summary>
        /// 
        /// </summary>
        public void StopMouseLeaveEventFromFiring()
        {
            delayMouseLeaveEventTimer.Stop();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void targetNotifyIcon_MouseMove(object sender, MouseEventArgs e)
        {
            notifyIconMousePosition = Control.MousePosition; 
            MouseMove(); 
            delayMouseLeaveEventTimer.Start(); 
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void delayMouseLeaveEventTimer_Tick(object sender, EventArgs e)
        {
            if (notifyIconMousePosition != System.Windows.Forms.Control.MousePosition)
            {
                MouseLeave(); 
                delayMouseLeaveEventTimer.Stop(); 
            }
        }

        #region IDisposable Members

        /// <summary>
        /// 
        /// </summary>
        private bool _IsDisposed = false;

        ~ExtendedNotifyIcon()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(true);
        }

        protected virtual void Dispose(bool IsDisposing)
        {
            if (_IsDisposed)
                return;

            if (IsDisposing)
            {
                targetNotifyIcon.Dispose();
            }

            _IsDisposed = true;

            #endregion
        }
    }
}
